package modellayer;

/**
 * This demo-class can calculate subtotals with and
 * without discounts.
 * @author (ucn-1semprojekt-dm78-gruppe1)
 *
 */
public class OrderLine
{
	
	private Item item;
	private int amount;
	private double subTotal;
	private double discountSubTotal;
	private String discountText;
	
	/**
	 * This constructor immediately calculate two
	 * different subtotals. One discounted subtotal
	 * and another non-discounted subtotal.
	 * The results are ready in the two methods
	 * <code>getDiscountSubtotal()</code> and
	 * <code>getSubtotal()</code>
	 * @param item the item
	 * @param amount the amount of these items 
	 */
	public OrderLine(Item item, int amount)
	{
		this.item = item;
		this.amount = amount;
		calculateSubs();
	}
	
	private void calculateSubs()
	{
		double normPrice = item.getPrice();
		subTotal = normPrice * amount;
		
		discountSubTotal = item.getDiscounts().subTotal(normPrice,amount);
		
		discountText = null;
		if (discountSubTotal < subTotal) //did we get any discounts?
			discountText = item.getDiscounts().toString(); //yes, tell us why!
	}
	
	/**
	 * @return a string representation of this object
	 */
	public String toString()
	{
		StringBuilder line = new StringBuilder();
		line.append(item.getDescription()+" Price: "+item.getPrice()+
				" ,Amount = "+amount+" SubTotal = "+subTotal+"\n");
		if (discountText != null) line.append("Discount(s): "+
				discountText+"\n"+"Discounted subTotal = "+discountSubTotal);
		return line.toString();
	}

	/**
	 * @return the subTotal
	 */
	public double getSubTotal()
	{
		return subTotal;
	}

	/**
	 * @return the discountSubtotal
	 */
	public double getDiscountSubtotal()
	{
		return discountSubTotal;
	}

}
